Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank / jupyter.cs / Examples |
This notebook shows how to create a cell magic for calling Online Python Tutor.
First, we define the code to embed the tutor, and register the magic:
from IPython.display import HTML, display
from IPython.core.magic import register_line_cell_magic
import urllib
@register_line_cell_magic
def tutor(line, cell):
code = urllib.urlencode({"code": cell})
display(HTML("""
<iframe width="800" height="500" frameborder="0"
src="http://pythontutor.com/iframe-embed.html#%s&py=2">
</iframe>
""" % code))
Next, we write some code:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
row = matrix[i]
sum += row[j]
print i, j
print sum
To see it execute line my line, we simply prefix the cell with our new %%tutor
cell magic:
%%tutor
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
sum = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
row = matrix[i]
sum += row[j]
print i, j
print sum